<template>
  <div>
    <div>
      <BaseDialog width="60%" :visible.sync="visibleEmploy" title="选择员工" destroyOnClose :containerStyle="{ height: 'calc(100% - 70px)', overflow: 'auto', paddingBottom: '70px' }">
        <employeeForm @selectedRow="selectEmployee" @close="closeEmploy" />
      </BaseDialog>
      <FormPanel ref="formPanel" :list="fromList" :cols="1" labelWidth="130" :isSubmitBtn="false"></FormPanel>
    </div>

    <div :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
      <AjaxButton type="primary" :disabled="newStatus" :click="saveDialog">保 存</AjaxButton>
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { queryModel } from '@/api/repair/repairBusiness/model/index.js';
import { getBrand, getSeries, getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import moment from 'moment';
import SERVER from '@/api/server';
import { getConfigHeaders } from '@/api/fetch';
import { jsonToHump } from '@/utils/index';
import axios from '@/api/fetch';
import { dictionary } from '@/utils/dictMixin';
import employeeForm from './employeeForm'
import { addEmp, editEmp, deleteEmp } from '@/api/claim/dealer/AuthMaintenance';
export default {
  props: ['rowData', 'type', 'getLevelData'],
  mixins: [dictionary],
  components: {
    employeeForm
  },
  data() {
    return {
      fromList: this.createFromList(),
      modelId: undefined,
      isShowGdcx: false,
      fileList: [],
      getConfigHeaders: getConfigHeaders,
      newStatus: false,
      formLabelPro: {},
      visibleEmploy: false,//员工选择页面
      selectEmployList: [] //选中的员工
    };
  },

  computed: {
  },
  mounted() {
    if (this.type === 'edit') {
      console.log(this.fromList)
      this.fromList.forEach(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
    }
  },
  created() {
    // this.fromList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
  },
  methods: {

    //选择员工
    selectEmployee(val) {
      this.selectEmployList = val
      let arr = val.map(function (item, index) {
        return { employeeName: item.employeeName, position: item.position }
      })
      console.log(val, arr)
      this.visibleEmploy = false
      this.fromList.find(x => x.fieldName === 'empName').initialValue = arr[0].employeeName;
      this.fromList.find(x => x.fieldName === 'position').initialValue = arr[0].position;
    },
    createFromList() {
      return [
        {
          type: 'INPUT',
          label: '员工姓名',
          fieldName: 'empName',
          readonly: true, // 只读
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.addPeople()}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '岗位',
          fieldName: 'position',
          readonly: true,
          disabled: true,
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '预授权级别',
          fieldName: 'name',
          itemList: this.getLevelData,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          initialValue: ''
        }
        // {
        //   type: 'SELECT',
        //   label: '预授权类型',
        //   fieldName: 'authType',
        //   itemList: this.createDictList('5508'),
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        //   initialValue: ''
        // }
      ];
    },
    // getDisable(){
    //   if(this.type === 'edit'){
    //     return true
    //   }else {
    //     return false
    //   }
    // },
    addPeople() {
      // if(this.type==='add') {
      this.visibleEmploy = true
      // }
    },
    async saveDialog() {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();// 获取from改变时整个表单的值
      if (err) return;
      let params = JSON.parse(JSON.stringify(data));
      if (this.rowData.authType) {
        delete this.rowData.authType
      }
      params = Object.assign({}, this.rowData, params);
      if (this.selectEmployList.length) {
        params.empCode = this.selectEmployList[0].employeeNo;
      }
      this.getLevelData.forEach((item) => {
        if (item.text === params.name || item.value === params.name) {
          params.lId = item.value;
          params.name = item.text;
        }
      });
      if (this.type === 'add') {
        let res = await addEmp(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '保存成功!!' });
          this.$emit('close');
        }
      } else if (this.type === 'edit') {
        //编辑
        let res = await editEmp(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '修改成功!!' });
          this.$emit('close');
        }
      }
    },
    //关闭人员选择页面
    closeEmploy() {
      this.visibleEmploy = false
    },
    closeDialog() {
      this.$emit('close');
    },
  }
};
</script>
